package com.ttt.gdfood.controller;

import com.ttt.gdfood.entity.Image;
import com.ttt.gdfood.entity.Note;
import com.ttt.gdfood.service.MinioService;
import com.ttt.gdfood.service.NoteService;
import com.ttt.gdfood.util.JsonResult;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@RequestMapping("/note")
@CrossOrigin
public class NoteController {
    @Autowired
    NoteService noteService;

    @GetMapping("/getAll")
    public List<Note> getAll() {
        return noteService.noteShow();
    }

    @GetMapping("/getNoteByRegion")
    public List<Note> getNoteByRegion(@PathParam("region") String region) {
        return noteService.getNoteByRegion(region);
    }

    // 根据用户id找笔记
    @GetMapping("/getNoteById")
    public List<Note> getNoteById(@PathParam("id") int id) {
        return noteService.getNoteById(id);
    }

    @GetMapping("/getNoteByNoteId")
    public Note getNoteByRegion(@PathParam("id") int id) {
        return noteService.getNoteByNoteId(id);
    }

    @GetMapping("/{keyWord}")
    public List<Note> getNoteByKeyWord(@PathVariable("keyWord") String keyWord) {
        return noteService.getNoteByKeyWord(keyWord);
    }

    @DeleteMapping("/deleteById")
    public JsonResult<Void> deleteById(@PathParam("id") Integer id) {
        // 创建返回值
        JsonResult<Void> result = new JsonResult<Void>();
        try {
// 调用业务对象执行注册
            noteService.deleteById(id);
            // 响应成功
            result.setState(200);
            result.setMessage("删除成功");
        } catch (Exception e) {
// 删除异常
            result.setState(500);
            result.setMessage("删除失败");
        }
        return result;
    }

    @PostMapping("/addNote")
    public JsonResult<Void> addNote(@RequestBody Note note) {
        // 创建返回值
        JsonResult<Void> result = new JsonResult<Void>();
        try {
// 调用业务对象执行添加
            noteService.addNote(note);
            // 响应成功
            result.setState(200);
            result.setMessage("发表成功");
        } catch (Exception e) {
// 添加异常
            result.setState(500);
            result.setMessage("发表失败");
        }
        return result;
    }
//    @PostMapping("/addNote")
//    public List<Image> addNote(@RequestBody Note note) {
//        return noteService.addNote(note);
//    }

//    @PostMapping("/addImage")
//    public List<String> addImage(@RequestBody List<Image> image){
//       // minioService.uploadImage(image);
//        return minioService.uploadImage(image);
//    }
}
